package com.iafme.service;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.iafme.dto.res.FileResDTO;
import com.systemframework.oss.OssConfigure;
import com.systemframework.upload.FileUploadConfig;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author summer
 * @version 1.0
 * @date 2025/3/25 14:28
 */
@Service
@AllArgsConstructor
public class ZipFileService {

    private final FileUploadConfig fileUploadConfig;

    private final OssConfigure ossConfigure;

    public List<FileResDTO> zip(MultipartFile file) {
        String upload = fileUploadConfig.upload(file);
        File unzip = ZipUtil.unzip(upload, Charset.defaultCharset());
        List<File> files = FileUtil.loopFiles(unzip);
        List<FileResDTO> fileResDTOS = ListUtil.list(true);
        for (File file1 : files) {
           if(!file1.getPath().contains("__MACOSX")){
               String s = ossConfigure.uploadFile(file1);
               FileResDTO fileResDTO=new FileResDTO();
               fileResDTO.setName(file1.getName());
               fileResDTO.setUrl(s);
               fileResDTOS.add(fileResDTO);
           }
        }
        FileUtil.del(unzip);
        FileUtil.del(upload);
        System.out.println(files.size());
        return fileResDTOS;
    }
}
